using System;
using System.Collections.Generic;
using System.IO;
using System.Printing;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Xps.Packaging;
using Common;
using SExperiment;

namespace Report
{
	public class ExportReport
	{
		public static int PrintSampleResult(Experiment experiment, List<Sample> samples, string templatePath, PatientReportCreator.GetSamplePicture getSamplePicture)
		{
			string systemTempFile = Utility.GetSystemTempFile();
			string text = systemTempFile + ".xps";
			string outWordPath = string.Empty;
			ExportSampleResultToXPS(experiment, samples, templatePath, text, getSamplePicture, out outWordPath);
			PrintFile(text);
			return 0;
		}

		public static string CreateSampleResultXPSDocument(Experiment experiment, List<Sample> samples, string templatePath, PatientReportCreator.GetSamplePicture getSamplePicture, out string outWordPath)
		{
			outWordPath = string.Empty;
			string text = Utility.GetSystemTempFile() + ".xps";
			if (ExportSampleResultToXPS(experiment, samples, templatePath, text, getSamplePicture, out outWordPath) == -1)
			{
				return null;
			}
			return text;
		}

		public static int ExportSampleResultToXPS(Experiment experiment, List<Sample> samples, string templatePath, string xpsPath, PatientReportCreator.GetSamplePicture getSamplePicture, out string outWordPath)
		{
			string systemTempFile = Utility.GetSystemTempFile();
			outWordPath = systemTempFile + ".docx";
			if (ExportSampleResultToDocx(experiment, samples, templatePath, getSamplePicture, outWordPath) == -1)
			{
				return -1;
			}
			try
			{
				WordOperator.ExportToXPS(outWordPath, xpsPath);
			}
			catch (Exception ex)
			{
				Log.WriteLine(ex.StackTrace);
				return -1;
			}
			return 0;
		}

		public static int ExportSampleResultToDocx(Experiment experiment, List<Sample> samples, string templatePath, PatientReportCreator.GetSamplePicture getSamplePicture, string outWordPath)
		{
			if (!File.Exists(templatePath))
			{
				return -1;
			}
			try
			{
				File.Copy(templatePath, outWordPath, true);
				File.SetAttributes(outWordPath, File.GetAttributes(outWordPath) & ~FileAttributes.ReadOnly);
				PatientReportCreator patientReportCreator = new PatientReportCreator();
				patientReportCreator.Experiment = experiment;
				patientReportCreator.Samples = samples;
				patientReportCreator.WrodFile = outWordPath;
				patientReportCreator.ExportSamplesResultToWord(getSamplePicture);
			}
			catch (Exception ex)
			{
				Log.WriteLine(ex.StackTrace);
				return -1;
			}
			return 0;
		}

		public static void ExportExperimentReportToExcel(Experiment experiment, string path)
		{
			ExcelOperator.ExportToExcel(experiment, path);
		}

		public static void PrintExperimentTable(Experiment experiment)
		{
			string systemTempFile = Utility.GetSystemTempFile();
			string text = systemTempFile + ".xlsx";
			string text2 = systemTempFile + ".xps";
			ExcelOperator.ExportToExcel(experiment, text);
			ExcelOperator.ExportToXPS(text, text2);
			PrintFile(text2);
		}

		public static void PrintFile(string path, bool landscape = false)
		{
			PrintDialog printDialog = new PrintDialog();
			printDialog.PrintTicket.PageOrientation = (landscape ? PageOrientation.Landscape : PageOrientation.Portrait);
			if (printDialog.ShowDialog() == true)
			{
				XpsDocument xpsDocument = new XpsDocument(path, FileAccess.Read);
				FixedDocumentSequence fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
				printDialog.PrintDocument(fixedDocumentSequence.DocumentPaginator, "Print");
			}
		}

		private static void PrintVisual(Visual visual)
		{
			PrintDialog printDialog = new PrintDialog();
			printDialog.PrintVisual(visual, "Print");
		}
	}
}
